<?php
/**
 * @author Vanadia
 * @copyright Jiuchi Information Technology Co.,Ltd
 */
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Default controller class
 */
class Circle_management extends CI_Controller {
	/**
	 * Construnct function
	 */
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url_helper');
		$this->load->library('session');
		$uid = $this->session->userdata('admin_uid');
		if(empty($uid)){
		    redirect(site_url('admin/login'));
		}
		$this->load->model('user_model');
		$this->load->model('circle_model');
		$this->load->model('circle_category_model');
		$this->load->model('circle_user_model');
		$this->load->model('circle_applicant_model');
		$this->load->model('topic_model');
		$this->load->model('comments_model');
		$this->load->model('reply_model');
	}
	
	public function index(){
	    $res = $this->circle_model->getAllCircle();
	    $circleCategory = $this->circle_category_model->retrieve();
	    $cirCate = array();
	    foreach($circleCategory as $row){
	    	$cirCate[$row['id']] = $row['name'];
	    }
		$data = array(
			'cirCate'		=> $cirCate,
		    'circles'		=> $res,
	    	'adminname'		=> array('uname' => $this->session->userdata('admin_name')),
	    );
	    $this->load->view('admin/circle_management/circle_manage',$data);
	}
	
	public function userCreate(){
		$data = array(
				'adminname'		=> array('uname' => $this->session->userdata('admin_name')),
		);
	    $this->load->view('admin/sys_user_management/sys_userCreate',$data);
	}
	
	public function addSys_user()
	{
	    $sys_userInfo =array(
	        'account'   =>$this->input->post('account'),
	        'name'     =>$this->input->post('name'),
	        'password' =>md5(md5($this->input->post('password')).'qiwi'),
	        'email' =>$this->input->post('email'),
	        'created_at'	=>	date('Y-m-d H:i:s', time())
	    );
	    $res = $this->sys_user_model->createSys_user($sys_userInfo);
	    if($res){
	        die("<script>alert('提交成功');window.location.href='".site_url('admin/sys_user_management')."';</script>");
	    }
	}
	
	public function edit_Sys_user($sys_userId) {
	    $sys_userInfo = $this->sys_user_model->getUserById($sys_userId);
	    if ($sys_userInfo) {
	        $data = array(
				'adminname'		=> array('uname' => $this->session->userdata('admin_name')),
	            'sys_user' => $sys_userInfo
	        );
	    }else{
	        redirect('admin/sys_user_management/');
	    }
	    $this->load->view('admin/sys_user_management/edit_sys_user',$data);
	}
	
	public function updateSys_user($sys_userId){
	    $sys_userInfo = array(
	        'account'   =>$this->input->post('account'),
	        'name'     =>$this->input->post('name'),
	        'password' =>md5(md5($this->input->post('password')).'qiwi'),
	        'email' =>$this->input->post('email'),
	        'created_at'	=>	date('Y-m-d H:i:s', time())
	    );
	    if($sys_userInfo){
	        $this->sys_user_model->updateUserInfo($sys_userId,$sys_userInfo);
	        die("<script>alert('提交成功');window.location.href='".site_url('admin/sys_user_management/')."';</script>");
	    }
	}
	
	public function del_Sys_user($id)
	{
	    $res = $this->sys_user_model->updateUserInfo($id,array('deleted'=>1));
	    if($res){
	        die("<script>alert('删除成功');window.location.href='".site_url('/admin/sys_user_management/')."';</script>");
	    }
	}
	
	
	/*-------------------------------------------------*/
	/**
	 * Circle detail
	 */
	public function edit_circle($cid){
		$circleInfo = $this->circle_model->getInfo($cid);
		$data = array(
				'circleInfo'	=> $circleInfo,
				'adminname'		=> array('uname' => $this->session->userdata('admin_name')),
				'circleId'		=> $cid
		);
		$this->load->view('admin/circle_management/detail/circle_base',$data);
	}
	
	public function circle_topics($cid){
		$count = -1;
		$circleInfo = $this->circle_model->getInfo($cid);
		$res = $this->topic_model->retrieve($count, NULL, NULL, NULL, array('circle_id' => $cid, 'deleted'=>0));
		$topics = array();
		foreach ($res as $topic){
			$uInfo = $this->user_model->getInfo($topic['user_id']);
			$topics[] = array(
					'id'			=>	$topic['id'],
					'name'			=>	$uInfo['nickname'],
					'title'			=>	$topic['title'],
					'comments_number'	=>	$topic['comments_number'],
					'collection_number'	=>	$topic['collection_number'],
					'created_at'	=>	$topic['created_at']
			);
		}
		$data = array(
				'topics'		=> $topics,
				'adminname'		=> array('uname' => $this->session->userdata('admin_name')),
				'circleId'		=> $cid
		);
		$this->load->view('admin/circle_management/detail/circle_topics',$data);
	}
	
	public function circle_members($cid){
		$count = -1;
		$circleInfo = $this->circle_model->getInfo($cid);
		$res = $this->circle_user_model->retrieve($count, NULL, NULL, NULL, array('circle_id' => $cid));
		$members = array();
		foreach ($res as $member){
			$uInfo = $this->user_model->getInfo($member['user_id']);
			$members[] = array(
					'id'			=>	$member['id'],
					'uid'			=>	$uInfo['id'],
					'name'		=>	$uInfo['nickname'],
					'is_joined'	=>	$member['is_joined'],
					'created_at'	=>	$member['created_at']
			);
		}
		$data = array(
				'members'		=> $members,
				'adminname'		=> array('uname' => $this->session->userdata('admin_name')),
				'circleId'		=> $cid
		);
		$this->load->view('admin/circle_management/detail/circle_members',$data);
	}
	
	public function circle_applicant($cid){
		$count = -1;
		$res = $this->circle_applicant_model->retrieve($count, NULL, NULL, NULL, array('circle_id' => $cid, 'is_reviewed' => 0));
		$applicants = array();
		foreach ($res as $applicant){
			$uInfo = $this->user_model->getInfo($applicant['user_id']);
			$circleInfo = $this->circle_model->getInfo($applicant['circle_id']);
			$applicants[] = array(
					'id'			=>	$applicant['id'],
					'applicant'		=>	$uInfo['nickname'],
					'circle_name'	=>	$circleInfo['name'],
					'created_at'	=>	$applicant['created_at']
			);
		}
		$data = array(
				'applicants'	=> $applicants,
				'adminname'		=> array('uname' => $this->session->userdata('admin_name')),
				'circleId'		=> $cid
		);
		$this->load->view('admin/circle_management/detail/circle_applicant',$data);
	}
	
	public function applyAgree($aid){
		$applyInfo = $this->circle_applicant_model->getInfo($aid);
		$cid = $applyInfo['circle_id'];
		$uid = $applyInfo['user_id'];
		$circleInfo = $this->circle_model->getInfo($cid);
		
		$this->circle_user_model->begin();
		$data = array(
				'circle_id'	=>	$cid,
				'user_id'	=>	$uid
		);
		if($this->circle_user_model->checkIsConcerned($uid, $cid) != TRUE){
			$res = $this->circle_user_model->createEntity($data);
			if($res <= 0){
				die("<script>alert('处理失败，请稍后重试！');window.history.back();</script>");
			}
		}
		$res = $this->circle_applicant_model->updateInfo($aid,array('is_reviewed' => 1));
		if(!$res){
			$this->circle_user_model->rollback();
			die("<script>alert('处理失败，请稍后重试！');window.history.back();</script>");
		}
		$res = $this->circle_user_model->joincircle($uid, $cid);
		if(!$res){
			$this->circle_user_model->rollback();
			die("<script>alert('处理失败，请稍后重试！');window.history.back();</script>");
		}
		$res = $this->circle_model->updateInfo($cid,array('user_number' => ($circleInfo['user_number']+1)));
		if($res <= 0){
			$this->circle_user_model->rollback();
			die("<script>alert('处理失败，请稍后重试！');window.history.back();</script>");
		}
		
		$this->circle_user_model->commit();
		die("<script>alert('处理成功！');window.history.back();</script>");
	}
	
	public function applyRefuse($aid){
		$res = $this->circle_applicant_model->refuseApply($aid);
		if($res){
			die("<script>alert('处理成功！');window.history.back();</script>");
		}else{
			die("<script>alert('拒绝失败，请稍后重试！');window.history.back();</script>");
		}
		
	}

	public function topicEdit($id){
		$count = -1;
		$topicInfo = $this->topic_model->getInfo($id);
		$res = $this->comments_model->retrieve($count, NULL, NULL, NULL, array('topic_id' => $id, 'deleted'=>0));
		$comments = array();
		foreach ($res as $comment){
			$uInfo = $this->user_model->getInfo($comment['user_id']);
			$comments[] = array(
					'id'			=>	$comment['id'],
					'name'			=>	$uInfo['nickname'],
					'title'			=>	$comment['content'],
					'reply_number'	=>	$comment['reply_munber'],
					'likes_number'	=>	$comment['likes_number'],
					'created_at'	=>	$comment['created_at']
			);
		}
		$data = array(
				'comments'		=> $comments,
				'adminname'		=> array('uname' => $this->session->userdata('admin_name')),
				'topicId'		=> $id
		);
		$this->load->view('admin/circle_management/detail/topic_comments',$data);

	}

	public function topicDel($id){
		$res = $this->topic_model->markDeleted($id);
		if($res){
			die("<script>alert('处理成功！');window.history.back();</script>");
		} else{
			die("<script>alert('删除失败，请稍后重试！');window.history.back();</script>");
		}
	}

	public function commentEdit($id){
		$count = -1;
		$commentInfo = $this->comments_model->getInfo($id);
		$res = $this->reply_model->retrieve($count, NULL, NULL, NULL, array('comment_id' => $id, 'deleted'=>0));
		$replies = array();
		foreach ($res as $reply){
			$ruInfo = $this->user_model->getInfo($reply['reply_user_id']);
			$uInfo = $this->user_model->getInfo($reply['user_id']);
			$replies[] = array(
					'id'			=>	$reply['id'],
					'name'			=>	$uInfo['nickname'],
					'title'			=>	$reply['content'],
					'reply_user'	=>	isset($ruInfo['nickname'])?$ruInfo['nickname']:'',
					'created_at'	=>	$reply['created_at']
			);
		}
		$data = array(
				'replies'		=> $replies,
				'adminname'		=> array('uname' => $this->session->userdata('admin_name')),
				'commentId'		=> $id
		);
		$this->load->view('admin/circle_management/detail/comment_replies',$data);

	}

	public function commentDel($id){
		$res = $this->comment_model->markDeleted($id);
		if($res){
			die("<script>alert('处理成功！');window.history.back();</script>");
		} else{
			die("<script>alert('删除失败，请稍后重试！');window.history.back();</script>");
		}
	}

	public function replyDel($id){
		$res = $this->reply_model->markDeleted($id);
		if($res){
			die("<script>alert('处理成功！');window.history.back();</script>");
		} else{
			die("<script>alert('删除失败，请稍后重试！');window.history.back();</script>");
		}
	}
}